package com.sbeam.service;

import com.sbeam.mapper.FactoryMapper;
import com.sbeam.mapper.GamesMapper;
import com.sbeam.mapper.SortMapper;
import com.sbeam.pojo.Factory;
import com.sbeam.pojo.Games;
import com.sbeam.pojo.Sort;
import com.sbeam.vo.GamesShowVO;
import com.sbeam.vo.HomeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class HomeServiceImpl implements HomeService{

    private String serviceURL = "http://localhost:8888/images";

    @Autowired
    private SortMapper sortMapper;

    @Autowired
    private FactoryMapper factoryMapper;

    @Autowired
    private GamesMapper gamesMapper;

    @Override
    public HomeVo getHomeVo() {
        HomeVo homeVo = new HomeVo();
        homeVo.setSortList(sortMapper.getSortList());
        List<Games> gamesList = gamesMapper.getGamesList();
        List<GamesShowVO> gamesShowVOList = new ArrayList<>();
        for (Games game:gamesList){
            List<String> imageURL = new ArrayList<>();
            String images = game.getImages();
            if (images != null && !images.equals("")){
                List<String> list = new ArrayList<>(Arrays.asList(images.split(",")));
                for(String s:list){
                    String concat = serviceURL.concat(s);
                    imageURL.add(concat);
                }
                System.out.println(imageURL);
            }
            GamesShowVO gamesShowVO = new GamesShowVO();
            Sort sort = sortMapper.getSortById(game.getParentId());
            Factory factory = factoryMapper.getFactoryById(game.getFactoryId());
            gamesShowVO.setId(game.getId())
                    .setName(game.getName())
                    .setPrice(Double.valueOf(game.getPrice()/100))
                    .setImages(imageURL)
                    .setSort(sort.getSort())
                    .setFactoryName(factory.getFactoryName());
            gamesShowVOList.add(gamesShowVO);
        }
        homeVo.setGamesShowList(gamesShowVOList);
        return homeVo;
    }
}
